﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Serialization;

namespace OmegleConversationBuilder
{
    public partial class ConversationPartControl : UserControl
    {
        public delegate void RemovedHandler(ConversationPartControl sender);
        public event RemovedHandler Removed;

        public delegate void MovedHandler(ConversationPartControl sender);
        public event MovedHandler Moved;

        public DragAndDrop DragAndDropInfo { get; set; }
        public ConversationData ConversationInfo { get; set; }
        bool Initialized = false;

        [Serializable]
        public class ConversationData
        {
            public Triggers Trigger { get; set; }
            public object TriggerData { get; set; }
            public bool GenerateFromWPM { get; set; }
            public int TypingSeconds { get; set; }
            public string Message { get; set; }
        }

        public class DragAndDrop
        {
            public bool MoveEnabled { get; set; }
            public Point Offset { get; set; }
        }

        public enum Triggers
        {
            WaitForSeconds = 0,
            WaitForReply = 1,
            WaitForSpecificReply = 2,
        }
        public ConversationPartControl(ConversationData conversationInfo = null)
        {
            InitializeComponent();

            DragAndDropInfo = new DragAndDrop();

            if (conversationInfo == null)
            {
                ConversationInfo = new ConversationData();
                comboBoxTrigger.SelectedIndex = 0;
                comboBoxTyping.SelectedIndex = 0;
                UpdateSettings();
            }
            else
            {
                ConversationInfo = conversationInfo;
                LoadUIFromInfo();
            }
            Initialized = true;
        }

        void LoadUIFromInfo()
        {
            comboBoxTrigger.SelectedIndex = (int)ConversationInfo.Trigger;
            comboBoxTyping.SelectedIndex = ConversationInfo.GenerateFromWPM ? 0 : 1;
            numericUpDownTyping.Value = ConversationInfo.TypingSeconds;
            richTextBoxMessage.Text = ConversationInfo.Message;
            switch (ConversationInfo.Trigger)
            {
                case Triggers.WaitForSeconds:
                    numericUpDownSeconds.Value = (int)ConversationInfo.TriggerData;
                    break;
                case Triggers.WaitForReply:
                    checkBoxReply.Checked = (bool)ConversationInfo.TriggerData;
                    break;
                case Triggers.WaitForSpecificReply:
                    textBoxSpecificReply.Text = (string)ConversationInfo.TriggerData;
                    break;
            }
            UpdateTriggerLayout();
        }

        void UpdateTriggerLayout()
        {
            for (int i = 0; i < tableLayoutPanelTrigger.ColumnStyles.Count; i++)
                tableLayoutPanelTrigger.ColumnStyles[i].Width = (i == (int)ConversationInfo.Trigger) ? 100f : 0f;
        }

        void UpdateSettings()
        {
            ConversationInfo.Trigger = (Triggers)comboBoxTrigger.SelectedIndex;

            UpdateTriggerLayout();

            switch (ConversationInfo.Trigger)
            {
                case Triggers.WaitForSeconds:
                    ConversationInfo.TriggerData = (int)numericUpDownSeconds.Value;
                    break;
                case Triggers.WaitForReply:
                    ConversationInfo.TriggerData = checkBoxReply.Checked;
                    break;
                case Triggers.WaitForSpecificReply:
                    ConversationInfo.TriggerData = textBoxSpecificReply.Text;
                    break;
            }

            ConversationInfo.TypingSeconds = (int)numericUpDownTyping.Value;
            ConversationInfo.GenerateFromWPM = (comboBoxTyping.SelectedIndex == 0);

        }

        private void updateSettingsEvent(object sender, EventArgs e)
        {
            if (Initialized)
                UpdateSettings();
        }

        private void pictureBoxRemove_Click(object sender, EventArgs e)
        {
            Removed(this);
        }

        private void panelBar_MouseDown(object sender, MouseEventArgs e)
        {
            DragAndDropInfo.Offset = e.Location;
            DragAndDropInfo.MoveEnabled = true;
        }

        private void panelBar_MouseUp(object sender, MouseEventArgs e)
        {
            DragAndDropInfo.MoveEnabled = false;
            Moved(this);
        }

        private void panelBar_MouseLeave(object sender, EventArgs e)
        {
            Moved(this);
            DragAndDropInfo.MoveEnabled = false;
        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {
            ConversationInfo.Message = richTextBoxMessage.Text;
        }

    }
}
